---
- name: start httpd (provided in the apache role)
  service: name=httpd state=started

- name: ensure packages required for resultsdb are installed (yum)
  package: name={{ item }} state=present
  when: ansible_distribution_major_version|int < 22
  with_items:
    - resultsdb
    - mod_wsgi
    - python-psycopg2
    - libsemanage-python

- name: ensure packages required for resultsdb are installed (dnf)
  dnf: name={{ item }} state=present enablerepo={{ extra_enablerepos }}
  when: ansible_distribution_major_version|int > 21 and ansible_cmdline.ostree is not defined
  with_items:
    - resultsdb
    - mod_wsgi
    - python-psycopg2
    - libsemanage-python

- name: ensure database is created
  delegate_to: "{{ resultsdb_db_host_machine }}"
  become_user: postgres
  become: true
  postgresql_db: db={{ resultsdb_db_name }}

- name: ensure resultsdb db user has access to database
  delegate_to: "{{ resultsdb_db_host_machine }}"
  become_user: postgres
  become: true
  postgresql_user: db={{ resultsdb_db_name }} user={{ resultsdb_db_user }} password={{ resultsdb_db_password }} role_attr_flags=NOSUPERUSER

- name: ensure selinux lets httpd talk to postgres
  seboolean: name=httpd_can_network_connect_db persistent=yes state=yes

- name: generate resultsdb config
  template: src=settings.py.j2 dest=/etc/resultsdb/settings.py owner=root group=root mode=0644
  notify:
    - reload httpd

- name: generate resultsdb apache config
  template: src=resultsdb.conf.j2 dest=/etc/httpd/conf.d/resultsdb.conf owner=root group=root mode=0644
  notify:
    - reload httpd

# this command will clear the exising database and it's now being run every time, regardless of whether
# /etc/resultsdb-is-init exists or not. We've already lost a lot of data, so commenting this command out
# while we figure out how to handle the problem
#- name: initialize resultsdb database
#  shell: PROD='true' resultsdb init_db && touch /etc/resultsdb/db-is-init creates=/etc/resultsdb-is-init
